package com.sc.study.sleeproom.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sc.common.annotation.Log;
import com.sc.common.core.controller.BaseController;
import com.sc.common.core.domain.AjaxResult;
import com.sc.common.enums.BusinessType;
import com.sc.study.sleeproom.domain.ScSleepRoomPersonType;
import com.sc.study.sleeproom.service.IScSleepRoomPersonTypeService;
import com.sc.common.utils.poi.ExcelUtil;
import com.sc.common.core.page.TableDataInfo;

/**
 * 寝室类型Controller
 * 
 * @author sc
 * @date 2022-03-14
 */
@RestController
@RequestMapping("/study/sleeproompersontype")
public class ScSleepRoomPersonTypeController extends BaseController
{
    @Autowired
    private IScSleepRoomPersonTypeService scSleepRoomPersonTypeService;

    /**
     * 查询寝室类型列表
     */
    @PreAuthorize("@ss.hasPermi('study:sleeproompersontype:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScSleepRoomPersonType scSleepRoomPersonType)
    {
        startPage();
        List<ScSleepRoomPersonType> list = scSleepRoomPersonTypeService.selectScSleepRoomPersonTypeList(scSleepRoomPersonType);
        return getDataTable(list);
    }

    @GetMapping("/selectByIds")
    public AjaxResult selectByIds(String ids){
        List<Long> idList = JSON.parseArray(ids, Long.class);
        List<ScSleepRoomPersonType> scSleepRoomPersonTypes = new ArrayList<>();

        idList.forEach(id -> {
            scSleepRoomPersonTypes.add(scSleepRoomPersonTypeService.selectScSleepRoomPersonTypeById(id));
        });

        return AjaxResult.success(scSleepRoomPersonTypes);
    }

    /**
     * 导出寝室类型列表
     */
    @PreAuthorize("@ss.hasPermi('study:sleeproompersontype:export')")
    @Log(title = "寝室类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScSleepRoomPersonType scSleepRoomPersonType)
    {
        List<ScSleepRoomPersonType> list = scSleepRoomPersonTypeService.selectScSleepRoomPersonTypeList(scSleepRoomPersonType);
        ExcelUtil<ScSleepRoomPersonType> util = new ExcelUtil<ScSleepRoomPersonType>(ScSleepRoomPersonType.class);
        util.exportExcel(response, list, "寝室类型数据");
    }

    /**
     * 获取寝室类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('study:sleeproompersontype:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(scSleepRoomPersonTypeService.selectScSleepRoomPersonTypeById(id));
    }

    /**
     * 新增寝室类型
     */
    @PreAuthorize("@ss.hasPermi('study:sleeproompersontype:add')")
    @Log(title = "寝室类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScSleepRoomPersonType scSleepRoomPersonType)
    {
        return toAjax(scSleepRoomPersonTypeService.insertScSleepRoomPersonType(scSleepRoomPersonType));
    }

    /**
     * 修改寝室类型
     */
    @PreAuthorize("@ss.hasPermi('study:sleeproompersontype:edit')")
    @Log(title = "寝室类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScSleepRoomPersonType scSleepRoomPersonType)
    {
        return toAjax(scSleepRoomPersonTypeService.updateScSleepRoomPersonType(scSleepRoomPersonType));
    }

    /**
     * 删除寝室类型
     */
    @PreAuthorize("@ss.hasPermi('study:sleeproompersontype:remove')")
    @Log(title = "寝室类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scSleepRoomPersonTypeService.deleteScSleepRoomPersonTypeByIds(ids));
    }
}
